[% setvar title Perl syntax support for ranges %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Perl syntax support for ranges</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: pdl-porters team &lt;<a href='mailto:pdl-porters@jach.hawaii.edu'>pdl-porters@jach.hawaii.edu</a>&gt;
  Date: 16 Aug 2000
  Last Modified: 11 Sep 2000
  Mailing List: <a href='mailto:perl6-language-data@perl.org'>perl6-language-data@perl.org</a>
  Number: 117
  Version: 2
  Status: Withdrawn
  Superceded-by: RFC 81, v3</pre>
<a name='REASONS FOR WITHDRAWEL'></a><h1>REASONS FOR WITHDRAWEL</h1>
<p>This RFC has been withdrawn after all of its principal points
were amalgamated into RFC 81v3 <i>Lazily evaluated list generation
functions</i> which we hereby endorse. At the point in time when all
RFCs will be frozen or withdrawn it should be verified that RFC 81
still mentions the features requested below.</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes syntactic support for ranges. Range objects
would be especially useful to specify indexing ranges of compact
numerical arrays (currently implemented by PDL) in a concise
manner, e.g.</p>
<pre>  $y = $x-&gt;slice(0:$n-1:4);
  </pre>
<p>Note that currently (perl5) we have to say</p>
<pre>  $n1 = $n-1;  # since we need to stringify
  $y = $x-&gt;slice(&quot;0:$n1:4&quot;);</pre>
<p>This should be contrasted with the less cluttered syntax offered
by numerical Python and commercial systems such as Matlab and IDL:</p>
<pre>  y = x[0:n-1:4]; 
  </pre>
<p>In perl we desire to say:</p>
<pre> $y = $x(0:$n-1,4);  
 </pre>
<p>or even</p>
<pre> @y = @x[0:$n-1,4]; 
 </pre>
<p>If there is a more general unification of PDL arrays and normal
perl arrays. (See <i><a href='#RFC 203-207'>&quot;RFC 203-207&quot;</a></i>).</p>
<p>This RFC proposes ranges as part of the syntax cleanup, <i><a href='#RFC 115'>&quot;RFC 115&quot;</a></i>
proposes a way top overload <code>()</code> for objects. This RFC is closely
linked to RFCs 81 and 169 (and proposes a subset of the features in
those RFCs) but emphasizes two additional aspects: Motivation by
requirements of PDL and the important implementation aspect of
<i>infinitely lazy evaluation</i>. Ultimately we hope to retire this RFC
when RFCs 81 and 169 have been appropriately updated.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>A range would be an object that is different from current perl5
lists but could be interpolated into one if desired. The <code>:</code> operator
would be the range generating operator.</p>
<p>The behaviour would be similar to the '..' operator but generally
no explicit in-memory list would be generated. These ranges would be
very useful to concisely specify subslices into multi-dimensional
numerical arrays.</p>
<p>Perhaps the '..' operator can be recycled as
something else and ':' used for all ranges (or ':' simply be an alias
for '..').</p>
<a name='Examples:'></a><h2>Examples:</h2>
<pre>         :               # all the things on this dimension: full span
         5:-1            # 5..last
         5:-1:2          # Every second item, up to the last or second last
         -1:7:3          # Start with last item, then fourth last, etc. until 7

  for (0:7) { ... }
  for (0.1:1.0:0.1) { ... }

  $pdl-&gt;slice(-1:$n:3) .= 5;
  $pdl-&gt;slice(:,::2) *= 2;</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Possible. A range should probably be a lazily evaluated list which
functions can choose to accept without the need to actually generate
the list in memory. Compare the RFC on lazy list generation.</p>
<p>A detail that is crucial from our point of view that is not mentioned
in RFC 81 is the ability for a function to inquire the <code>start:stop:step</code>
parameters of the range and do its own thing without ever generating
a list or list iterator -- an implementation aspect we refer to as
<i>infinitely lazy evaluation</i>.</p>
<a name='Issues'></a><h2>Issues</h2>
<p>An issue is the treatment of potentially infinite ranges of the form</p>
<pre>  :
  :5
  3:</pre>
<p>Those could raise an error if used in a context where actual list
items are generated, e.g.</p>
<pre>   for (:) { $a *= $_ }</pre>
<p>but be allowed in circumstances where no explicit list item is
ever created, e.g.</p>
<pre>  $a-&gt;slice(:);
  $a-&gt;slice(-5:-2);</pre>
<p>which might actually be written in perl6 as</p>
<pre>  $a(:); # and/or
  @a[:];</pre>
<a name='SEE ALSO'></a><h1>SEE ALSO</h1>
<p><i><a href='http://search.cpan.org/perldoc?PDL'>PDL</a></i> (<a href='http://pdl.sourceforge.net/PDLdocs)' target='_blank'>pdl.sourceforge.net</a></p>
<p><a href='http://pdl.perl.org' target='_blank'>pdl.perl.org</a></p>
<p>RFC 24: Semi-finite (lazy) lists</p>
<p>RFC 81: Lazily evaluated list generation functions</p>
<p>RFC 169: Proposed syntax for matrix element access and slicing</p>
<p>RFC 115: Overloadable parentheses for objects</p>
</div>
